package cn.imvc.app.models;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import cn.imvc.app.entities.Group;
import cn.imvc.app.entities.Role;
import cn.imvc.app.repositories.GroupRepository;
import cn.imvc.app.repositories.RoleRepository;
import cn.imvc.core.HttpContext;
@Service
public class RoleGroupModelImpl implements RoleGroupModel {

	@Autowired
	private RoleRepository roleRepository;
	@Autowired
	private GroupRepository groupRepository;
	
	public String getRoles() {
		List<Role> roleList=roleRepository.findAll(new Sort(Direction.DESC,"order"));
		StringBuffer roles=new StringBuffer();
		for (Role role : roleList) {
			roles.append("<li><a href='"+HttpContext.getRootUrl()+"/system/role-group/"+role.getId()+"' target='ajax' rel='groupTree'>"+role.getName()+"</a></li>");
		}
		return roles.toString();
		}

	public Role getRole(int id) {
		return roleRepository.findOne(id);
	}

	public String getGroupTree(int roleId) {
		StringBuffer tree = new StringBuffer();
		List<Group> groups=groupRepository.findAll();
		String checked="";
		Role role=getRole(roleId);
		for (Group group : groups) {
			if(group.getRole().contains(role)){
				checked="checked='true'";
			}else{
				checked="";
			}
			tree.append("<li><a tname='groupid' tvalue='"+group.getId()+"' "+checked+" >"+group.getName()+"</a></li>");
		}
		return tree.toString();
	}

	public void saveRoleGroup(int id, Set<Group> groups) {
		Role role=roleRepository.findOne(id);
		for (Object object : role.getGroup()) {
			Group group=(Group)object;
			group.getRole().remove(role);
		}
		role.getGroup().clear();
		roleRepository.saveAndFlush(role);
		role.getGroup().addAll(groups);
		for (Object object : role.getGroup()) {
			Group group=(Group)object;
			group.getRole().add(role);
		}
		roleRepository.saveAndFlush(role);
	}

	public String getGroups() {
		List<Group> groupList=groupRepository.findAll(new Sort(Direction.DESC,"order"));
		StringBuffer groups=new StringBuffer();
		for (Group group : groupList) {
			groups.append("<li><a href='"+HttpContext.getRootUrl()+"/system/group-role/"+group.getId()+"' target='ajax' rel='roleTree'>"+group.getName()+"</a></li>");
		}
		return groups.toString();
	}
	public String getRoleTree(int groupId) {
		StringBuffer tree = new StringBuffer();
		List<Role> roles=roleRepository.findAll();
		String checked="";
		Group group=groupRepository.findOne(groupId);
		for (Role role : roles) {
			if(role.getGroup().contains(group)){
				checked="checked='true'";
			}else{
				checked="";
			}
			tree.append("<li><a tname='roleid' tvalue='"+role.getId()+"' "+checked+" >"+role.getName()+"</a></li>");
		}
		return tree.toString();
	}

	public void saveGroupRole(int id, Set<Role> roles) {
		Group group=groupRepository.findOne(id);
		for (Object object : group.getRole()) {
			Role role=(Role)object;
			role.getGroup().remove(group);
		}
		group.getRole().clear();
		groupRepository.saveAndFlush(group);
		
		group.getRole().addAll(roles);
		
		for (Object object : group.getRole()) {
			Role role=(Role)object;
			role.getGroup().add(group);
		}
		groupRepository.saveAndFlush(group);
		
	}

}
